package com.clw.handler;

import com.clw.enums.ResultEnum;
import com.clw.exception.SellAuthorizeException;
import com.clw.exception.SellException;
import com.clw.utils.ResultVOUtils;
import com.clw.vo.ResultVO;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * 2017-07-30 17:44
 */
@ControllerAdvice
public class SellExceptionHandler {

    //拦截登录异常
    @ResponseBody
    @ExceptionHandler(value = SellAuthorizeException.class)
    public Object handlerAuthorizeException() throws IOException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getMethod().equals("POST")){
            return ResultVOUtils.error(ResultEnum.UNAUTHORIZED_ACCESS.getCode(),
                    ResultEnum.UNAUTHORIZED_ACCESS.getMessage());
        }
        return new ModelAndView("redirect:"+ "/");
    }

    @ResponseBody
    @ExceptionHandler(value = SellException.class)
    public ResultVO handlerSellException(SellException e){
        return ResultVOUtils.error(e.getCode(),e.getMessage());
    }
}
